package com.celebriosoftware.client;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.inputmethod.InputMethodManager;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;


/*
 * Class that allows communication from page javascript to android code
 * 
 * If webview.addJavascriptInterface(javascriptInterface, "name") is used,
 * then methods annotated @JavascriptInterface can be called in javascript as
 * name.methodname().
 */
public class JavaScriptInterface {
	private WebView engine;
	private String url;
	private MainActivity activity;
	
	public JavaScriptInterface(MainActivity activity, WebView engine) {
		this.url = MainActivity.BROWSER_DEFAULT_URL;
		this.activity = activity;
		this.engine = engine;
	}
	
	//Called from javascript, starts our desktop
	@JavascriptInterface
	public void toDesktop() {
		activity.startDesktop();
	}
	
	//Called from javascript, starts our desktop
	@JavascriptInterface
	public void toDesktop(String lang) {
		activity.startDesktop(lang);
	}
	
	//Indicates that background was changed and should be reloaded
	@JavascriptInterface
	public void invalidateBackground() {
		MainActivity.backgroundIsOK=false;
	}
	
	//Called from javascript, starts our browser
	@JavascriptInterface
	public String startBrowser() {
		activity.runOnUiThread(
			new Runnable() {
				public void run() {
					try {
						 Intent startNewActivityOpen = new Intent(activity,BrowserActivity.class);
						 startNewActivityOpen.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
						 activity.startActivity(startNewActivityOpen);
					} catch (Exception e) {
						System.err.println(e.toString());
					}
				}
			}
		);
		return this.url;
	}
	
	@JavascriptInterface
	public String startBrowser(String uri) {
		class StartNewBrowserTask implements Runnable {
			String uri;
			public StartNewBrowserTask(String uri) {
				this.uri=uri;
			}
			public void run() {
				try {
					 Intent startNewActivityOpen = new Intent(activity,BrowserActivity.class);
					 startNewActivityOpen.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					 startNewActivityOpen.putExtra("com.celebriosoftware.client.uri", uri);
					 activity.startActivity(startNewActivityOpen);
				} catch (Exception e) {
					System.err.println(e.toString());
				}				
			}			
		}
		activity.runOnUiThread(new StartNewBrowserTask(uri));
		return uri;
	}
	
	//Shows keyboard if called (from page that has one input field)
	@JavascriptInterface
	public void openKeyboard() {
	    InputMethodManager keyboard = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
	    keyboard.showSoftInput(engine, InputMethodManager.SHOW_IMPLICIT);
	}	
	
	//Starts video talker activity
	@JavascriptInterface
	public void openVideoTalker(String session, String token) {
		Intent startNewActivityOpen = new Intent(activity,VideoTalkerActivity.class);
		startNewActivityOpen.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startNewActivityOpen.putExtra("com.celebriosoftware.client.videoSession", session);
		startNewActivityOpen.putExtra("com.celebriosoftware.client.videoToken", token);
		activity.startActivity(startNewActivityOpen);
	}

/*	private static Bitmap getBitmapFromURL(String src) {
		try {
			URL url = new URL(src);
			HttpURLConnection connection = (HttpURLConnection) url
					.openConnection();
			connection.setDoInput(true);
			connection.connect();
			InputStream input = connection.getInputStream();
			Bitmap myBitmap = BitmapFactory.decodeStream(input);
			return myBitmap;
		} catch (IOException e) {
			e.printStackTrace();
			Log.e("Celebrio-Image", e.getMessage());
			return null;
		}
	}*/
    
	//alternative image uploading way, not used now
	//@JavascriptInterface
	//public void loadFileIntoGallery() {
	//	activity.loadFileIntoGallery();
	//}
	
	@JavascriptInterface
	public String getUrl() {
		return this.url;
	}

	@JavascriptInterface
	public void log(String text) {
		Log.i("Celebrio-Javascript", text);
	}
}
